package com.hanlin.beforejavacodesummary.unifyformat;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanlin.beforejavacodesummary.pojo.po.EcoAiDevice;
import com.hanlin.beforejavacodesummary.pojo.po.PersonDO;
import com.mysql.cj.xdevapi.JsonArray;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: lijing
 * @since: 2025-10-11
 * @description:
 */
@SpringBootTest
public class UnifyFormatJson {


    /*
    *
    * 测量装置 设备状态数据诊断 小模型  历史数据 解析为  统一标准 数据格式  尝试
    *
    * */
    @Test
    public void measuringEquipmentHistory() throws IOException {
        JSONObject unifyFormatMap = new JSONObject();

        unifyFormatMap.put("code",200);
        unifyFormatMap.put("msg","请求成功");
        unifyFormatMap.put("success",true);
        //timeSeries做成曲线；image-base64/image-url做成图文；
        unifyFormatMap.put("dataType","timeSeries");
        List<Map<String,Object>> historyMetrics = new ArrayList<>();
        unifyFormatMap.put("historyMetrics",historyMetrics);

        String astId = "astId";
        List<EcoAiDevice> equipNameList = new ArrayList<>();
        EcoAiDevice ecoAiDevice1 = new EcoAiDevice();
        ecoAiDevice1.setId("47");
        ecoAiDevice1.setName("A套");
        EcoAiDevice ecoAiDevice2 = new EcoAiDevice();
        ecoAiDevice2.setId("48");
        ecoAiDevice2.setName("B套");
        EcoAiDevice ecoAiDevice3 = new EcoAiDevice();
        ecoAiDevice3.setId("49");
        ecoAiDevice3.setName("C套");
        equipNameList.add(ecoAiDevice1);
        equipNameList.add(ecoAiDevice2);
        equipNameList.add(ecoAiDevice3);

        Map<String, String> nameMap = new HashMap<>();
        nameMap.put("47","history47");
        nameMap.put("48","history48");
        nameMap.put("49","history49");

        for (int i = 0; i < equipNameList.size(); i++) {
            EcoAiDevice ecoAiDevice = equipNameList.get(i);
            String equipHistotyDataName = nameMap.get(ecoAiDevice.getId());
            ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/measuringEquipment/" + equipHistotyDataName + ".json");
            JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));

            JSONArray checkResultsJsonArray = httpJsonObject.getJSONObject("data").getJSONArray("checkResults");
            for (int j = 0; j < checkResultsJsonArray.size(); j++) {
               Map<String, Object> histotyGroupData = new HashMap<>();

                JSONObject checkResultsItemJsonObject = checkResultsJsonArray.getJSONObject(j);
//近30天历史数据
                histotyGroupData.put("hisTimeLongData",checkResultsItemJsonObject.getString("hisTimeLongData"));
//近24小时历史数据
                histotyGroupData.put("hisTimeShortData",checkResultsItemJsonObject.getString("hisTimeShortData"));
//平均值
                histotyGroupData.put("hisFamilyData",checkResultsItemJsonObject.getString("hisFamilyData"));

                for (Map.Entry<String, Object> entry : histotyGroupData.entrySet()) {
                    //这个就是历史数据的名称
                    String key = entry.getKey();
                    if (entry.getValue() != null) {
                        //这个就是历史数据的集合  TODO  historyArrayData 中的元素暂时没有处理成  time 、 value 格式
                        JSONArray historyArrayData = JSONObject.parseArray((String) entry.getValue());

                        JSONObject historyMetricsItemJsonObcect = new JSONObject();
                        historyMetricsItemJsonObcect.put("unit",checkResultsItemJsonObject.getString("unit"));
                        historyMetricsItemJsonObcect.put("metricEn","");
//metricCh =  设备 name + “-” + 标准名字
                        String metricCh = ecoAiDevice.getName() + "-" + checkResultsItemJsonObject.getString("analogName");
                        historyMetricsItemJsonObcect.put("metricCh",metricCh);
                        historyMetricsItemJsonObcect.put("data",historyArrayData);

//自己加的
                        historyMetricsItemJsonObcect.put("modelCriterion",checkResultsItemJsonObject.getString("analogName"));
                        historyMetricsItemJsonObcect.put("deviceName",ecoAiDevice.getName());
                        historyMetricsItemJsonObcect.put("dataType",key);
                        historyMetrics.add(historyMetricsItemJsonObcect);
                    }
                }
            }
        }
        System.out.println("测量装置历史数据统一格式处理结果为：");
        System.out.println(unifyFormatMap);
    }


    /*
     *
     * 测量装置 设备状态数据诊断 小模型  诊断数据 解析为  统一标准 数据格式  尝试
     *
     * */
    @Test
    public void measuringEquipmentDiagnose() throws IOException {
        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/measuringEquipment/" + "diagnose47-48-49" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));

        JSONObject unifyFormatMap = new JSONObject();

        unifyFormatMap.put("code",200);
        unifyFormatMap.put("msg","请求成功");
        unifyFormatMap.put("success",true);
        List<Map<String,Object>> dataList = new ArrayList<>();
        unifyFormatMap.put("data",dataList);

        JSONArray checkResultsJsonArray = httpJsonObject.getJSONObject("data").getJSONArray("checkResults");
        for (int i = 0; i < checkResultsJsonArray.size(); i++) {
            JSONObject checkResultsItemJsonObject = checkResultsJsonArray.getJSONObject(i);

//          小模型标准的整体诊断结果 是否有异常。
            String result = checkResultsItemJsonObject.getString("result");
//          标准的名字
            String name = checkResultsItemJsonObject.getString("name");
//          此标准下 每个 设备的 诊断结果
            JSONArray detailsJsonArray = checkResultsItemJsonObject.getJSONArray("details");
            List<JSONObject> detailsList = new ArrayList<>();
            //存放 统一接口以外的信息
            List<List<JSONObject>> extInfoAll = new ArrayList<>();

            for (int j = 0; j < detailsJsonArray.size(); j++) {
                List<JSONObject> extInfo = new ArrayList<>();
                JSONObject detailsJsonArrayItem = detailsJsonArray.getJSONObject(j);

                String deviceDiagnoseResult = detailsJsonArrayItem.getString("result");
                if("严重".equals(deviceDiagnoseResult)){
                    detailsJsonArrayItem.put("resultCode",3);
                } else if ("异常".equals(deviceDiagnoseResult)) {
                    detailsJsonArrayItem.put("resultCode",2);
                } else if ("正常".equals(deviceDiagnoseResult)) {
                    detailsJsonArrayItem.put("resultCode",1);
                }else {
                    //没有 匹配到约定的 设备诊断结果
                    detailsJsonArrayItem.put("resultCode",0);
                }
                detailsList.add(detailsJsonArrayItem);
//                存放额外信息
                JSONObject extInfoItem1 = new JSONObject();
                extInfoItem1.put("extName","deviceId");
                extInfoItem1.put("extValue",detailsJsonArrayItem.getString("deviceId"));
                extInfo.add(extInfoItem1);
                JSONObject extInfoItem2 = new JSONObject();
                extInfoItem2.put("extName","treatment");
                extInfoItem2.put("extValue",detailsJsonArrayItem.getString("treatment"));
                extInfo.add(extInfoItem2);
                JSONObject extInfoItem3 = new JSONObject();
                extInfoItem3.put("extName","faultComponent");
                extInfoItem3.put("extValue",detailsJsonArrayItem.getString("faultComponent"));
                extInfo.add(extInfoItem3);
                extInfoAll.add(extInfo);
            }
            detailsList.sort((obj1, obj2) ->
                    Integer.compare(obj2.getIntValue("resultCode"), obj1.getIntValue("resultCode"))
            );


            String description = getDiagnoseDescription(detailsList,name);

            JSONObject dataListItemJsonObject = new JSONObject();

            dataListItemJsonObject.put("description",description);
            //小模型整体的 诊断结果
            dataListItemJsonObject.put("result",result);
            //具体的 设备中 诊断 结果最严重的一个。  看看用哪个 具体在讨论
            //dataListItemJsonObject.put("result",detailsList.get(0).getString("result"));
            dataListItemJsonObject.put("extInfo",extInfoAll);
            dataListItemJsonObject.put("metricEn"," ");
            dataListItemJsonObject.put("metricCh",name);
            dataList.add(dataListItemJsonObject);
        }
        // 还差一个综合输出
        JSONObject synthesizedOutputJsonObject = new JSONObject();
        String[] chineseNumbers = {"(一）", "(二）", "(三）", "(四）", "(五）", "(六）", "(七）", "(八）", "(九）", "(十）"};
        StringBuilder sb = new StringBuilder();
        String synthesizedOutputResult = "正常";
        for (int i = 0; i < dataList.size(); i++) {
            String chineseNumber = chineseNumbers[i];
            Map<String, Object> deviceDiagnoseMap = dataList.get(i);
            String result = (String) deviceDiagnoseMap.get("result");
            sb.append(chineseNumber);
            sb.append(deviceDiagnoseMap.get("metricCh").toString());
            sb.append(result);
            sb.append("<br/>");
            sb.append(deviceDiagnoseMap.get("description").toString());
            // 正常   异常  严重
            if (("严重".equals(result) || "异常".equals(result)) && !"严重".equals(synthesizedOutputResult)) {
                synthesizedOutputResult = result;
            }
        }
        synthesizedOutputJsonObject.put("metricCh","综合输出");
        synthesizedOutputJsonObject.put("result",synthesizedOutputResult);
        synthesizedOutputJsonObject.put("description",sb.toString());
        dataList.add(synthesizedOutputJsonObject);

        System.out.println(unifyFormatMap);



    }

    private String getDiagnoseDescription(List<JSONObject> detailsList, String name) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < detailsList.size(); i++) {
            JSONObject detailsItemJsonObject = detailsList.get(i);

            sb.append(detailsItemJsonObject.getString("deviceName"));
            sb.append("-");
            sb.append(name);
            sb.append(": ");
            sb.append(detailsItemJsonObject.getString("reason"));
            sb.append("<br/>");
        }

        return sb.toString();
    }

    /*
     *
     * 平高开关 SF6气体 诊断 小模型  历史数据 解析为  统一标准 数据格式  尝试
     *
     * */
    @Test
    public void pingGaoSwitchHistory() throws IOException {
        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/pingGaoSwitch/" + "SF6_History" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));

        JSONObject unifyFormatMap = new JSONObject();

        unifyFormatMap.put("code",200);
        unifyFormatMap.put("msg","请求成功");
        unifyFormatMap.put("success",true);
        //timeSeries做成曲线；image-base64/image-url做成图文；
        unifyFormatMap.put("dataType","timeSeries");
        List<Map<String,Object>> historyMetrics = new ArrayList<>();
        unifyFormatMap.put("historyMetrics",historyMetrics);

        JSONArray dataArray = httpJsonObject.getJSONArray("data");

        for (int i = 0; i < dataArray.size(); i++) {
//            mutually 翻译为 相
            JSONObject mutuallyJsonObject = dataArray.getJSONObject(i);
            String type = mutuallyJsonObject.getString("type");
            String extInfo = mutuallyJsonObject.getString("extInfo");
            JSONArray originalHistoryMetrics = mutuallyJsonObject.getJSONArray("historyMetrics");
            for (int j = 0; j < originalHistoryMetrics.size(); j++) {
                JSONObject originalHistoryMetricsItemJsonObject = originalHistoryMetrics.getJSONObject(j);
                JSONObject historyMetricsItem = new JSONObject();
                historyMetrics.add(historyMetricsItem);

//              小模型接口返回的数据
                String metricType = originalHistoryMetricsItemJsonObject.getString("metricType");//sf6： 小模型的 英文简称
                String metricName = originalHistoryMetricsItemJsonObject.getString("metricName");//密度
                String unit = originalHistoryMetricsItemJsonObject.getString("unit");//单位
                JSONArray metrics = originalHistoryMetricsItemJsonObject.getJSONArray("metrics");//数据

                // 统一格式 组装的数据
                historyMetricsItem.put("unit",unit);
                historyMetricsItem.put("data",metrics);
                //英文名 暂时空着，不知道填什么
                historyMetricsItem.put("metricEn","");
                historyMetricsItem.put("metricCh",type + "-" + metricName);

                //以下三个是自己加的，为什么要加，后面需要这些数据给word模版中填充图片，不加，没法画图。
                historyMetricsItem.put("modelCriterion",metricName);
                //平高开关历史数据 只有 一组，所以这个 dataType 在这里没有用到。
                historyMetricsItem.put("dataType","");
                historyMetricsItem.put("deviceName",type);
            }


        }
        System.out.println(unifyFormatMap);

    }
    /*
     *
     * 平高开关 SF6气体 诊断 小模型  诊断数据 解析为  统一标准 数据格式  尝试
     *
     * */
    @Test
    public void pingGaoSwitchDiagnose() throws IOException {

        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/pingGaoSwitch/" + "SF6_Diagnose" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));

        JSONObject unifyFormatMap = new JSONObject(httpJsonObject);

        System.out.println(unifyFormatMap);

    }



    /*
     *
     * 平高开关 SF6气体 诊断 小模型  实时数据 解析为  统一标准 数据格式  尝试
     *
     * */
    @Test
    public void pingGaoSwitchActual() throws IOException {
        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/pingGaoSwitch/" + "SF6_Actual" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));

        JSONObject unifyFormatMap = new JSONObject();
        unifyFormatMap.put("code",200);
        unifyFormatMap.put("msg","请求成功");
        unifyFormatMap.put("success",true);
//  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatMap.put("dataType","timeSeries");

        List<Map<String,Object>> originData = new ArrayList<>();
        unifyFormatMap.put("originData",originData);


        JSONArray dataArray = httpJsonObject.getJSONArray("data");
        for (int i = 0; i < dataArray.size(); i++) {
            // 每个相的 实时数据
            JSONObject mutuallyJsonObject = dataArray.getJSONObject(i);

            String type = mutuallyJsonObject.getString("type");//A相
            String extInfo = mutuallyJsonObject.getString("extInfo");//A相
            // 同一个相下 每个标准的数据，有三个标准就有三个 对象。
            JSONArray originCheckData = mutuallyJsonObject.getJSONArray("originCheckData");
            for (int j = 0; j < originCheckData.size(); j++) {
                JSONObject actualJsonObject = originCheckData.getJSONObject(j);
                String metricType = actualJsonObject.getString("metricType");//sf6
                String metricName = actualJsonObject.getString("metricName");//日泄漏率
                String code = actualJsonObject.getString("code");//null
                String value = actualJsonObject.getString("value");//0.8583
                String unit = actualJsonObject.getString("unit");//"MPa"
                String time = actualJsonObject.getString("time");//2025-08-07 12:49:00

                JSONObject originDataItemJsonObject = new JSONObject();
                originDataItemJsonObject.put("metricEn"," ");
                originDataItemJsonObject.put("metricCh",type + "-" + metricName);
                originDataItemJsonObject.put("time",time);
                originDataItemJsonObject.put("value",value);
                originDataItemJsonObject.put("unit",unit);
                //自己添加的  加这个type 是为了好分组， 好找 同一个相 下的数据
                originDataItemJsonObject.put("type",type);

                originData.add(originDataItemJsonObject);
            }
        }

        System.out.println(unifyFormatMap);

    }







    /*
     *
     * 普通开关  诊断 小模型  实时数据 - 历史数据 解析为  统一标准 数据格式  尝试
     * 普通开关的 实时数据和历史数据 放到一个接口中返回了。
     * */
    @Test
    public void CommonSwitchActualAndHistory() throws IOException {

        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/commonSwitch/" + "0201-A-H-Data" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));
//实时数据
        JSONObject unifyFormatActualMap = new JSONObject();
        unifyFormatActualMap.put("code",200);
        unifyFormatActualMap.put("msg","请求成功");
        unifyFormatActualMap.put("success",true);
//  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatActualMap.put("dataType","timeSeries");

        JSONObject checkResultJsonObject = httpJsonObject.getJSONArray("checkResult").getJSONObject(0);

        String extInfo = checkResultJsonObject.getString("extInfo");//重燃
        String type = checkResultJsonObject.getString("type");//null
        //实时数据
        JSONArray orignCheckDataJsonArray = checkResultJsonObject.getJSONArray("orignCheckData");

        //统一格式封装数据
        List<JSONObject> originData = new ArrayList<>();
        unifyFormatActualMap.put("originData",originData);
        for (int i = 0; i < orignCheckDataJsonArray.size(); i += 2) {
            JSONObject phaseItem = orignCheckDataJsonArray.getJSONObject(i);
            JSONObject phaseDataItem = orignCheckDataJsonArray.getJSONObject(i+1);

            JSONObject originDataItem = new JSONObject();

            originDataItem.put("metricEn","");
            String metricCh = phaseItem.getString("value");//A
            String metricName = phaseDataItem.getString("metricName");//重燃击穿时间
            originDataItem.put("metricCh",metricCh + "相-" + metricName);
            originDataItem.put("time",phaseDataItem.getString("time"));
            originDataItem.put("value",phaseDataItem.getString("value"));
            originDataItem.put("unit",phaseDataItem.getString("unit"));

            originData.add(originDataItem);
        }
        System.out.println("运行数据：实时数据：");
        System.out.println(unifyFormatActualMap);




        JSONObject unifyFormatHistoryMap = new JSONObject();
        unifyFormatHistoryMap.put("code",200);
        unifyFormatHistoryMap.put("msg","请求成功");
        unifyFormatHistoryMap.put("success",true);
//  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatHistoryMap.put("dataType","timeSeries");

        List<JSONObject> unifyHistoryMetrics = new ArrayList<>();
        unifyFormatHistoryMap.put("historyMetrics",unifyHistoryMetrics);
        //小模型 历史数据
        JSONArray historyMetrics = checkResultJsonObject.getJSONArray("historyMetrics");

        for (int i = 0; i < historyMetrics.size(); i++) {
            JSONObject historyMetricsItemJsonObject = historyMetrics.getJSONObject(i);

            JSONObject unifyHistoryMetricsItemJsonObject = new JSONObject();
            String metricName = historyMetricsItemJsonObject.getString("metricName");//电压
            String metricType = historyMetricsItemJsonObject.getString("metricType");//A-voltage

            String phase = metricType.substring(0, metricType.indexOf("-"));//A
            String metricCh = phase + "相-" + metricName;
            //此 相下 此 标准下 具体的历史数据。
            JSONArray metrics = historyMetricsItemJsonObject.getJSONArray("metrics");
            //处理具体的历史数据
            List<JSONObject> dataList = new ArrayList<>();
            for (int j = 0; j < metrics.size(); j++) {
                JSONObject dataListItem = new JSONObject();
                JSONObject metricsItemJsonObject = metrics.getJSONObject(j);

                dataListItem.put("time",metricsItemJsonObject.getString("code"));
// 设置value字段（使用原value字段的值，并转换为数字类型）
                String valueStr = metricsItemJsonObject.getString("value");
                try {
                    // 尝试转换为double类型
                    double numericValue = Double.parseDouble(valueStr);
                    dataListItem.put("value", numericValue);
                } catch (NumberFormatException e) {
                    // 如果转换失败，保留原值
                    dataListItem.put("value", valueStr);
                }
                dataList.add(dataListItem);
            }
            unifyHistoryMetricsItemJsonObject.put("metricEn",metricType);
            unifyHistoryMetricsItemJsonObject.put("metricCh",metricCh);
            unifyHistoryMetricsItemJsonObject.put("unit",historyMetricsItemJsonObject.getString("unit"));
            unifyHistoryMetricsItemJsonObject.put("data",dataList);
            unifyHistoryMetrics.add(unifyHistoryMetricsItemJsonObject);

        }

        System.out.println("运行数据：历史数据：");
        System.out.println(unifyFormatHistoryMap);

    }

    /*
     *
     * 普通开关  诊断 小模型  诊断数据 解析为  统一标准 数据格式  尝试
     * 普通开关的 实时数据和历史数据 放到一个接口中返回了。
     * */
    @Test
    public void CommonSwitchDiagnose() throws IOException {

        ClassPathResource modelHistotyData = new ClassPathResource("unifyformat/commonSwitch/" + "0201-D-Data" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));
//实时数据
        JSONObject unifyFormatMap = new JSONObject();
        unifyFormatMap.put("code", 200);
        unifyFormatMap.put("msg", "请求成功");
        unifyFormatMap.put("success", true);
        List<JSONObject> dataList = new ArrayList<>();
        unifyFormatMap.put("data", dataList);

        //诊断数据
        JSONArray checkResultJsonArray = httpJsonObject.getJSONArray("checkResult");

        for (int i = 0; i < checkResultJsonArray.size(); i++) {
            //模型下标准诊断数据
            JSONObject checkResultItemJsonObject = checkResultJsonArray.getJSONObject(i);

            String result = checkResultItemJsonObject.getString("result");//正常
            String reason = checkResultItemJsonObject.getString("reason");
            String extInfo = checkResultItemJsonObject.getString("extInfo");// 合闸角度 -> 标准名字
            String time = checkResultItemJsonObject.getString("time"); //"2024-02-21 17:11:41"


            //组装诊断 统一格式结果数据
            JSONObject dataListItem = new JSONObject();
            dataListItem.put("metricEn"," ");
            dataListItem.put("metricCh",extInfo);
            dataListItem.put("result",result);
            dataListItem.put("description",reason);
            List<JSONObject> extInfoList = new ArrayList<>();
            JSONObject extInfo1 = new JSONObject();
            extInfo1.put("extName","time");
            extInfo1.put("extValue",time);
            extInfoList.add(extInfo1);
            dataListItem.put("extInfo",extInfoList);

            dataList.add(dataListItem);
        }

        //还差一个综合 结论。
// 还差一个综合输出
        JSONObject synthesizedOutputJsonObject = new JSONObject();
        String[] chineseNumbers = {"(一）", "(二）", "(三）", "(四）", "(五）", "(六）", "(七）", "(八）", "(九）", "(十）"};
        StringBuilder sb = new StringBuilder();
        String synthesizedOutputResult = "正常";
        for (int i = 0; i < dataList.size(); i++) {
            String chineseNumber = chineseNumbers[i];

            JSONObject deviceDiagnoseMap = dataList.get(i);
            String result =  deviceDiagnoseMap.getString("result");
            sb.append(chineseNumber);
            sb.append(deviceDiagnoseMap.getString("metricCh"));
            sb.append(result);
            sb.append("<br/>");
            sb.append(deviceDiagnoseMap.getString("description"));
            // 正常   异常  严重
            if (("严重".equals(result) || "异常".equals(result)) && !"严重".equals(synthesizedOutputResult)) {
                synthesizedOutputResult = result;
            }
        }
        synthesizedOutputJsonObject.put("metricCh","综合输出");
        synthesizedOutputJsonObject.put("result",synthesizedOutputResult);
        synthesizedOutputJsonObject.put("description",sb.toString());
        dataList.add(synthesizedOutputJsonObject);

        System.out.println(unifyFormatMap);

    }


    /*
    *
    *以绍兴 换流变为案例：
    * 实际中没有用到的模型：
    * 0104 局放 诊断模版
    * 0105 视觉诊断
    * 0106 实验数据诊断
    * 0107 机械特性诊断 模版
    *
    *
    * 可以 解析 0102  和 0103
    * 我们这里 解析的是 0102
    * */

    @Test
    public void converterTransformerActualAndHistory() throws IOException {

        ClassPathResource  modelHistotyData = new ClassPathResource("unifyformat/converterTransformer/" + "0102-A-H-Data" + ".json");
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));
        //实时数据
        JSONObject unifyFormatActualMap = new JSONObject();
        unifyFormatActualMap.put("code",200);
        unifyFormatActualMap.put("msg","请求成功");
        unifyFormatActualMap.put("success",true);
        //  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatActualMap.put("dataType","timeSeries");

        List<JSONObject> originDataList = new ArrayList<>();
        unifyFormatActualMap.put("originData",originDataList);

        JSONObject checkResultJsonObject = httpJsonObject.getJSONArray("checkResults").getJSONObject(0);

        //小模型 实时数据
        JSONArray orignCheckDataJsonArray = checkResultJsonObject.getJSONArray("orignCheckData");

        for (int i = 0; i < orignCheckDataJsonArray.size(); i++) {
            //每个标准的 实时数据
            JSONObject checkResultItemJsonObject = orignCheckDataJsonArray.getJSONObject(i);
            String metricType = checkResultItemJsonObject.getString("metricType");//power
            String metricName = checkResultItemJsonObject.getString("metricName");//功率
            String code = checkResultItemJsonObject.getString("code");//2025-06-26 14:54:40
            String value = checkResultItemJsonObject.getString("value");//"996.1185"
            String dataDw = checkResultItemJsonObject.getString("dataDw");//"MW"

            JSONObject originDataListItem = new JSONObject();

            originDataListItem.put("metricEn",metricType);
            originDataListItem.put("metricCh",metricName); //这个小模型 没有  相
            originDataListItem.put("time",code);

            try {
                // 尝试转换为double类型
                double numericValue = Double.parseDouble(value);
                originDataListItem.put("value", numericValue);
            } catch (NumberFormatException e) {
                // 如果转换失败，保留原值
                originDataListItem.put("value",value);
            }
            originDataListItem.put("unit",dataDw);
            originDataList.add(originDataListItem);
        }

        System.out.println("换流变 实时数据：");
        System.out.println(unifyFormatActualMap);




        //历史数据
        JSONObject unifyFormatHistoryMap = new JSONObject();
        unifyFormatHistoryMap.put("code",200);
        unifyFormatHistoryMap.put("msg","请求成功");
        unifyFormatHistoryMap.put("success",true);
        //  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatHistoryMap.put("dataType","timeSeries");

        List<JSONObject> historyMetricsList = new ArrayList<>();
        unifyFormatHistoryMap.put("historyMetrics",historyMetricsList);

        //历史数据
        JSONArray historyMetricsDataJsonArray = checkResultJsonObject.getJSONArray("historyMetrics");

        for (int i = 0; i < historyMetricsDataJsonArray.size(); i++) {
            JSONObject historyMetricsItemJsonObject = historyMetricsDataJsonArray.getJSONObject(i);
            String metricType = historyMetricsItemJsonObject.getString("metricType");
            String metricName = historyMetricsItemJsonObject.getString("metricName");
            String dataDw = historyMetricsItemJsonObject.getString("dataDw");
            JSONArray metrics = historyMetricsItemJsonObject.getJSONArray("metrics");

            List<JSONObject> dataList = new ArrayList<>();
            for (int j = 0; j < metrics.size(); j++) {
                JSONObject metricsItem = metrics.getJSONObject(j);

                JSONObject dataListItem = new JSONObject();
                dataListItem.put("time",metricsItem.getString("code"));
                String stringValue = metricsItem.getString("value");
                Double numericValue = Double.valueOf(stringValue);
                dataListItem.put("value",numericValue);
                dataList.add(dataListItem);


            }

            JSONObject historyMetricsListItem = new JSONObject();
            historyMetricsListItem.put("metricEn",metricType);
            historyMetricsListItem.put("metricCh",metricName);
            historyMetricsListItem.put("unit",dataDw);
            historyMetricsListItem.put("data",dataList);

            historyMetricsList.add(historyMetricsListItem);


        }

        System.out.println("换流变 历史数据：");
        System.out.println(unifyFormatHistoryMap);

    }





    @Test
    public void converterTransformerDiagnose() throws IOException {

        ClassPathResource modelHistotyData = new ClassPathResource("unifyformat/converterTransformer/" + "0102-D-Data" + ".json");
        String path = modelHistotyData.getPath();
        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));
        //实时数据
        JSONObject unifyFormatMap = new JSONObject();
        unifyFormatMap.put("code", 200);
        unifyFormatMap.put("msg", "请求成功");
        unifyFormatMap.put("success", true);
        //  timeSeries做成图表；image-base64/image-url做成图文
        unifyFormatMap.put("dataType", "timeSeries");

        List<JSONObject> dataList = new ArrayList<>();
        unifyFormatMap.put("data", dataList);
        //诊断数据
        JSONArray checkResultsJsonArray = httpJsonObject.getJSONArray("checkResults");
        for (int i = 0; i < checkResultsJsonArray.size(); i++) {
            JSONObject checkResultsJsonArrayItem = checkResultsJsonArray.getJSONObject(i);
            String result = checkResultsJsonArrayItem.getString("result");
            String reason = checkResultsJsonArrayItem.getString("reason");
            String orignCheckData = checkResultsJsonArrayItem.getString("orignCheckData");
            String extInfo = checkResultsJsonArrayItem.getString("extInfo");
            String historyMetrics = checkResultsJsonArrayItem.getString("historyMetrics");

            JSONObject dataListItem = new JSONObject();
            dataListItem.put("metricEn","");
            dataListItem.put("metricCH","");
            dataListItem.put("result",result);
            dataListItem.put("description",reason);
            dataListItem.put("extInfo",new ArrayList<>());
            dataList.add(dataListItem);
        }

// 还差一个综合输出
        JSONObject synthesizedOutputJsonObject = new JSONObject();
        String[] chineseNumbers = {"(一）", "(二）", "(三）", "(四）", "(五）", "(六）", "(七）", "(八）", "(九）", "(十）"};
        StringBuilder sb = new StringBuilder();
        String synthesizedOutputResult = "正常";
        for (int i = 0; i < dataList.size(); i++) {
            String chineseNumber = chineseNumbers[i];

            JSONObject deviceDiagnoseMap = dataList.get(i);
            String result =  deviceDiagnoseMap.getString("result");
            sb.append(chineseNumber);
            sb.append(deviceDiagnoseMap.getString("metricCh"));
            sb.append(result);
            sb.append("<br/>");
            sb.append(deviceDiagnoseMap.getString("description"));
            // 正常   异常  严重
            if (("严重".equals(result) || "异常".equals(result)) && !"严重".equals(synthesizedOutputResult)) {
                synthesizedOutputResult = result;
            }
        }
        synthesizedOutputJsonObject.put("metricCh","综合输出");
        synthesizedOutputJsonObject.put("result",synthesizedOutputResult);
        synthesizedOutputJsonObject.put("description",sb.toString());
        dataList.add(synthesizedOutputJsonObject);

        System.out.println(unifyFormatMap);
    }


    @Test
    public void testOpenJson() throws IOException {
        ClassPathResource modelHistotyData = new ClassPathResource("simulate-shanghai/" + "0101-Y-D" + ".json");

        JSONObject httpJsonObject = JSONObject.parseObject(StreamUtils.copyToString(modelHistotyData.getInputStream(), StandardCharsets.UTF_8));
        System.out.println(httpJsonObject.getString("code"));

        //实时数据
    }



    private Map<String, Object> getRunDiagnoseData(List<JSONObject> resultList) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("status","01");
        resultMap.put("reason","暂无数据");
        resultMap.put("result","正常");
        resultMap.put("whetherExistData", "ExistData.SUCCESS.getValue()");
        String[] chineseNumbers = {"(一）", "(二）", "(三）", "(四）", "(五）", "(六）", "(七）", "(八）", "(九）", "(十）"};
        StringBuilder sb = new StringBuilder();
        String synthesizedOutputResult = "正常";
        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                String chineseNumber = chineseNumbers[i];

                JSONObject deviceDiagnoseMap = resultList.get(i);
                String result =  deviceDiagnoseMap.getString("result");
                sb.append(chineseNumber);
                sb.append(deviceDiagnoseMap.getString("devTypeName"));
                sb.append(result);
                sb.append("<br/>");
                sb.append(deviceDiagnoseMap.getString("description"));
                // 正常   异常  严重
                if (("严重".equals(result) || "异常".equals(result)) && !"严重".equals(synthesizedOutputResult)) {
                    synthesizedOutputResult = result;
                    resultMap.put("status","02");
                    resultMap.put("result",result);
                }
            }
            resultMap.put("reason",sb.toString());
        }

        return resultMap;


    }

}
